﻿using Com.Ctrip.Framework.Apollo;
using Com.Ctrip.Framework.Apollo.Enums;
using Com.Ctrip.Framework.Apollo.Logging;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ManaMaple.Apollo
{
    public static class ApolloService
    {
        public static void AddApolloService(this IHostBuilder hostBuilder)
        {
            hostBuilder.ConfigureAppConfiguration((hostBuilderContext, configurationBuilder) =>
            {
                //日志
                LogManager.UseConsoleLogging(Com.Ctrip.Framework.Apollo.Logging.LogLevel.Info);
                var configBuilder = new ConfigurationBuilder();
                // 下面这两句话效果一样环境变量和appsettings.json都读的到
                var apollo = hostBuilderContext.Configuration.GetSection("Apollo");
                if (!string.IsNullOrWhiteSpace(apollo.Value))
                {
                    //var apollo = configurationBuilder.Build().GetSection("Apollo");
                    var config = configBuilder.AddApollo(apollo).AddDefault().Build();
                    ApolloAppsettingsHelper.SetConfiguration(config);
                    configurationBuilder.AddApollo(apollo).AddDefault().Build();
                }
            });
        }
    }
}
